package com.ruoyi.entity.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.entity.domain.Image;
import com.ruoyi.entity.service.IImageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 图片Controller
 *
 * @author Delamer
 * @date 2020-12-23
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/entity/image")
public class ImageController extends BaseController {

    private final IImageService imageService;

    /**
     * 查询图片列表
     */
    @PreAuthorize("@ss.hasPermi('entity:image:list')")
    @GetMapping("/list")
    public TableDataInfo list(Image image) {
        startPage();
        LambdaQueryWrapper<Image> lqw = new LambdaQueryWrapper<Image>();
        if (image.getUserId() != null) {
            lqw.eq(Image::getUserId, image.getUserId());
        }
        if (StringUtils.isNotBlank(image.getUserName())) {
            lqw.like(Image::getUserName, image.getUserName());
        }
        if (image.getPicId() != null) {
            lqw.eq(Image::getPicId, image.getPicId());
        }
        if (StringUtils.isNotNull(image.getPicPage())) {
            lqw.eq(Image::getPicPage, image.getPicPage());
        }
        if (StringUtils.isNotBlank(image.getPicTitle())) {
            lqw.like(Image::getPicTitle, image.getPicTitle());
        }
        if (image.getPicStatus() != null) {
            lqw.eq(Image::getPicStatus, image.getPicStatus());
        }
        if (image.getPicR18() != null) {
            lqw.eq(Image::getPicR18, image.getPicR18());
        }
        if (StringUtils.isNotBlank(image.getPicTags())) {
            lqw.like(Image::getPicTags, image.getPicTags().replace(" ", "%"));
        }
        lqw.orderByDesc(Image::getId);
        List<Image> list = imageService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出图片列表
     */
    @PreAuthorize("@ss.hasPermi('entity:image:export')")
    @Log(title = "图片", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Image image) {
        LambdaQueryWrapper<Image> lqw = new LambdaQueryWrapper<Image>(image);
        List<Image> list = imageService.list(lqw);
        ExcelUtil<Image> util = new ExcelUtil<Image>(Image.class);
        return util.exportExcel(list, "image");
    }

    /**
     * 获取图片详细信息
     */
    @PreAuthorize("@ss.hasPermi('entity:image:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(imageService.getById(id));
    }

    /**
     * 新增图片
     */
    @PreAuthorize("@ss.hasPermi('entity:image:add')")
    @Log(title = "图片", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Image image) {
        return toAjax(imageService.save(image) ? 1 : 0);
    }

    /**
     * 修改图片
     */
    @PreAuthorize("@ss.hasPermi('entity:image:edit')")
    @Log(title = "图片", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Image image) {
        return toAjax(imageService.updateById(image) ? 1 : 0);
    }

    /**
     * 删除图片
     */
    @PreAuthorize("@ss.hasPermi('entity:image:remove')")
    @Log(title = "图片", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(imageService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
